/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brucexx.wedding.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.brucexx.wedding.dao.BaseModelDAO;
import com.brucexx.wedding.model.Comment;
import com.brucexx.wedding.util.BeanHelper;
import com.brucexx.wedding.util.DateHelper;
import com.brucexx.wedding.util.StringHelper;
import com.google.appengine.api.datastore.Text;

/**
 * 
 * @author Administrator
 */
@Controller
public class BlessController {

	private static final Log logger = LogFactory.getLog(BlessController.class);

	@RequestMapping(value = "/bless.action", method = RequestMethod.GET)
	public String loadWall(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		return "bless/bless_wall";
	}

	@RequestMapping(value = "/add_bless.action", method = RequestMethod.GET)
	public void addBless(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String name = StringHelper.null2String(request.getParameter("name"));
		if (StringHelper.isEmpty(name)) {
			name = "匿名";
		}
		String content = StringHelper.null2String(request
				.getParameter("content"));
		String lp = StringHelper.null2String(request.getParameter("lp"));
		String type = StringHelper.null2String(request.getParameter("type"));
		Comment comment = new Comment();
		comment.setUserName(name);
		comment.setCommentContent(new Text(content));
		comment.setGmtCreate(new Date());
		comment.setTopic("04");
		comment.setCoordinate(lp);
		comment.setType(Integer.parseInt(type));

		try {
			BaseModelDAO.addOrUpdate(comment);
			response.getWriter().write("1");
		} catch (Exception e) {
			logger.error("添加评论异常", e);
			response.getWriter().write("0");
		}
	}

	@RequestMapping(value = "/load_bless.action", method = RequestMethod.GET)
	public void loadbless(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String gql = "select  from " + Comment.class.getName() + " where topic=='04'";
		JSONArray jsonList = new JSONArray();
		List<Comment> list = BaseModelDAO.executeSqlForList(gql);

		for (Comment comment : list) {
			JSONObject tmp = new JSONObject();
			BeanHelper.copyProperties2JSON(tmp, comment,"commentContent","gmtCreate");
			tmp.put("commentContent",comment.getCommentContent().getValue() );
			tmp.put("gmtCreate",DateHelper.getChinaDateTime(comment.getGmtCreate()));
			jsonList.add(tmp);
		}
		response.getWriter().write(jsonList.toString());
	}

}
